package com.yycwxt.modules.admin.data_import;

import com.yycwxt.common.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("data_import")
@Api("数据导入")
public class ImpFileController {
    private final ImpFileService impFileService;

    @Value("${file.location}")
    String filelocation;
    @Value("${file.path}")
    String filepath;

    public ImpFileController(ImpFileService prmFileService) {
        this.impFileService = prmFileService;
    }

    @GetMapping("/impFiles")
    @ApiOperation("获取页面显示数据")
    public ReturnResult selectShowFile(@ApiParam(name = "账期", required = true, type = "int") Integer zq) {
        if (zq == -1) {
            zq = AccountData.getAccountDate();
        }
        return ReturnResult.buildSuccessResult(impFileService.selectImpFiles(zq));
    }

    @PostMapping("/erpStoreOrder")
    @ApiOperation("ERP仓库订单上传")
    public ReturnResult FileUpLoad(@ApiParam(value = "ERP仓库订单excel文件", type = "_file") @RequestPart("file") MultipartFile file) {
        // 文件上传明细
        ImpFile impFile = new ImpFile();
        // 保存出错行
        List<ERPStoreOrder> erpStoreOrderList = new ArrayList<>();
        if (file.isEmpty()) {
            return ReturnResult.buildFailureResult("文件为空");
        } else {
            try {
                // 保存文件
                // 服务器文件名
                String serverFilename = UUID.randomUUID().toString().substring(0, 8) + ".xlsx";
                // 创建目录
                String path = filelocation + AccountData.getAccountDate();
                System.out.println(path);
                Files.createDirectories(Paths.get(path));
                // 保存
                file.transferTo(Path.of(path, serverFilename));

                // 记录上传文件明细
                impFile.setFilename(file.getOriginalFilename());
                impFile.setName("ERP仓库订单");
                impFile.setUrl("download/" + AccountData.getAccountDate() + "/" + serverFilename);
                impFile.setZq(AccountData.getAccountDate());
                impFile.setCreated(LocalDateTime.now());
                impFile.setRemark("包材费用，预估快递费用，操作费");
                System.out.println(impFile);
                impFileService.save(impFile);

                // 解析excel，记录流水
                impFileService.importErpStoreOrder(file, erpStoreOrderList);

                // 没有出错行
                if (erpStoreOrderList.size() == 0) {
                    return ReturnResult.buildSuccessResult("导入成功", true);
                } else {
                    return ReturnResult.buildSuccessResult("导入成功，共有" + erpStoreOrderList.size() + "行出错", erpStoreOrderList);
                }
            } catch (Exception e) {
                impFileService.removeById(impFile);
                return ReturnResult.buildFailureResult(e.toString());
            }
        }
    }
}
